package cn.fscode.code.generator.controller.system;

import cn.fscode.commons.core.response.Resp;
import cn.fscode.commons.tool.core.StringUtils;
import cn.fscode.commons.tool.core.exception.BizException;
import cn.fscode.code.generator.model.po.SysDictItemPO;
import cn.fscode.code.generator.model.po.SysDictPO;
import cn.fscode.code.generator.service.ISysDictItemService;
import cn.fscode.code.generator.service.ISysDictService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 * 
 * @author shenguangyang
 */
@Api(tags = "字典条目管理")
@Validated
@RestController
@RequestMapping("/api/sys/dictItem")
public class SysDictItemController {
    @Resource
    private ISysDictItemService dictItemService;

    @Resource
    private ISysDictService dictService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Resp<IPage<SysDictItemPO>> page(SysDictItemPO dictData) {
        IPage<SysDictItemPO> list = dictItemService.page(dictData);
        return Resp.ok(list);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation("根据字典编码查询字典条目信息")
    @GetMapping(value = "/listDictItem/{dictCode}")
    public Resp<List<SysDictItemPO>> listDictItem(@NotEmpty(message = "不能为空")
                                                  @PathVariable(value = "dictCode") String dictCode) {
        List<SysDictItemPO> data = dictItemService.listByCode(dictCode);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<>();
        }
        return Resp.ok(data);
    }

    /**
     * 新增字典类型
     */
    @ApiOperation("新增字典条目")
    @PostMapping("/save")
    public Resp<?> save(@Validated @RequestBody SysDictItemPO dictItem) {
        SysDictPO dict = dictService.getById(dictItem.getDictId());
        if (dict == null) {
            throw new BizException("选择的字典不存在");
        }
        dictItem.setDictCode(dict.getDictCode());
        return dictItemService.saveDictItem(dictItem) > 0 ? Resp.ok() : Resp.fail("保存失败");
    }

    /**
     * 修改保存字典类型
     */
    @ApiOperation("修改保存字典条目")
    @PostMapping("/update")
    public Resp<?> update(@RequestBody SysDictItemPO dictItem) {
        // dict.setUpdateBy(SecurityUtils.getUserName());
        return dictItemService.updateDictItem(dictItem) > 0 ? Resp.ok() : Resp.fail("更新失败");
    }

    /**
     * 删除字典类型
     */
    @ApiOperation("批量删除字典条目")
    @DeleteMapping("/deleteBatch")
    public Resp<?> deleteBatch(@RequestParam(value = "ids") Long[] dictCodes) {
        dictItemService.deleteByIds(dictCodes);
        return Resp.ok();
    }

    /**
     * 删除字典类型
     */
    @ApiOperation("删除字典条目")
    @DeleteMapping("/delete")
    public Resp<?> delete(@RequestParam(value = "id") @NotEmpty(message = "请输入字典id") String dictId) {
        dictItemService.deleteById(Long.valueOf(dictId));
        return Resp.ok();
    }
}